package encheres.presentation.ui;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import encheres.exceptions.EncheresCorbaException;
import encheres.exceptions.EncheresUIException;
import encheres.manager.corba.I_ClientOperations;

public class UIConnexion extends JFrame {

	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabelLogin = null;
	private JLabel jLabelPassword = null;
	private JTextField jTextFieldLogin = null;
	private JPasswordField jPasswordField = null;
	private JButton jButtonConnexion = null;
	private JButton jButtonCreerCompte = null;
	/**
	 * This method initializes 
	 * 
	 */
	public UIConnexion() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        
        this.setTitle("Encheres - Authentification");
        this.setContentPane(getJContentPane());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.pack();
        
        this.setVisible(true);
        this.setSize(new Dimension(449, 227));
			
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabelPassword = new JLabel();
			jLabelPassword.setBounds(new Rectangle(20, 61, 86, 16));
			jLabelPassword.setText("Mot de passe :");
			jLabelLogin = new JLabel();
			jLabelLogin.setBounds(new Rectangle(20, 29, 38, 16));
			jLabelLogin.setText("Login :");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(55, 25, 320, 144));
			jPanel.setBorder(BorderFactory.createTitledBorder("Connexion"));

			jPanel.add(jLabelLogin, null);
			jPanel.add(jLabelPassword, null);
			jPanel.add(getJTextFieldLogin(), null);
			jPanel.add(getJPasswordField(), null);
			jPanel.add(getJButtonConnexion(), null);
			jPanel.add(getJButtonCreerCompte(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextFieldLogin	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldLogin() {
		if (jTextFieldLogin == null) {
			jTextFieldLogin = new JTextField();
			jTextFieldLogin.setBounds(new Rectangle(110, 28, 192, 20));
		}
		return jTextFieldLogin;
	}

	/**
	 * This method initializes jTextFieldPassword	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(110, 60, 192, 20));
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jButtonConnexion	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonConnexion() {
		if (jButtonConnexion == null) {
			jButtonConnexion = new JButton();
			jButtonConnexion.setBounds(new Rectangle(35, 103, 97, 26));
			jButtonConnexion.setText("Connecter");
			jButtonConnexion.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						String login = getJTextFieldLogin().getText();
						String password = String.valueOf(getJPasswordField().getPassword());
						
			            int idSession = CorbaManager.getManagerClient().connexion(login, password);
						switch (idSession) {
						case 0:
							throw new EncheresUIException();
						case -1:
							throw new EncheresCorbaException();
						default:
							new UIEncheres(idSession);
							break;
						}
						
					} catch (EncheresUIException uiException) {
						getJPasswordField().setText("");
						JOptionPane.showMessageDialog(jButtonConnexion.getRootPane(), "Cet utilisateur n'existe pas !", "Encheres", JOptionPane.WARNING_MESSAGE);
					} catch (EncheresCorbaException corbaException){
						getJPasswordField().setText("");
						JOptionPane.showMessageDialog(jButtonConnexion.getRootPane(), "Une erreur inconnue est arrivée !\n\n"+corbaException.getMessage(), "Encheres", JOptionPane.ERROR_MESSAGE);
					} catch (Exception e2) {
						e2.printStackTrace();
						getJPasswordField().setText("");
						JOptionPane.showMessageDialog(jButtonConnexion.getRootPane(), "Une erreur inconnue est arrivée !\n\n"+e2.getMessage(), "Encheres", JOptionPane.ERROR_MESSAGE);
					} 
				}
			});
		}
		return jButtonConnexion;
	}

	/**
	 * This method initializes jButtonCreerCompte	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCreerCompte() {
		if (jButtonCreerCompte == null) {
			jButtonCreerCompte = new JButton();
			jButtonCreerCompte.setBounds(new Rectangle(164, 103, 126, 26));
			jButtonCreerCompte.setText("Créer compte");
			jButtonCreerCompte.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new UICreateUser();
				}
			});
		}
		return jButtonCreerCompte;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
